/*This is the servlet code which handles the addition of broadcaster for a particular nym */
/*Author:Shreyas Kamath*/

import java.sql.*;
import java.util.Date;
import java.io.*;
import java.util.*;
import java.util.HashMap;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;

//This servlet is associated with the addbroadcaster.html page and handles the addition of the broadcaster to a particular nym.
public class AddBroadCasterServlet extends HttpServlet{

	//Vector that holds the name of the buddies
	public static Vector vector_Buddyname =new Vector(20,5);
	public static int iNumBuddies=0;	
		
	//Initialize the servlet	
	public void init(ServletConfig config)
	{
	  System.out.println("FriendsServlet: init()");
	}
	
	//This method obtains the parameters such as the name of the broadcaster to be added and the name of the nym to which the broadcaster is to be added.
	//It then calls the function which does that actual addition of the broadcaster to the nym in the database.
	
	public void doPost(HttpServletRequest request,HttpServletResponse response)    
		throws ServletException, IOException 
	{
		int ibuddyno;
		String strPrevNym=null;
		// set the response type
		response.setContentType("text/xml; charset=UTF-8"); 
		response.setHeader("Cache-Control","no-cache");
		String strNym=request.getParameter("nym");
		//String strNym="Home";
		String strBroadcasters=request.getParameter("broadcasters");
		//String strBroadcasters="FDIC,BBB";
		int success=this.AddBroadCasters(strNym,strBroadcasters);
		// obtain the writer to write the response
		PrintWriter out = response.getWriter();
		//Write the xml file,the success parameter determines if the broadcaster was sucessfully added to the nym or no.
		out.println("<?xml version=\"1.0\"?>");  
		out.println("<Success>");
		out.println("<valid>");
		out.println(success);
		out.println("</valid>");
		out.println("</Success>");
	}
		
		
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// Connect()
	// Connects to the database
	// return value of type Connection
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static Connection Connect()
	{
		Connection conn = null;		
		try
		{
			//Connect to the database
			String userName = "nettrust";
			String password = "nettrust06162006";
			String url= "jdbc:mysql://localhost/NetTrust";
			Class.forName ("com.mysql.jdbc.Driver").newInstance ();
			conn = DriverManager.getConnection (url, userName, password);
			System.out.println ("Database connection established");
		}
		catch( Exception e )
		{
			//Throw error saying connection failed.
			System.out.println("Connection failed");
		};  
		//return the connection parameter.
		return( conn );
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * ** * */
	// end of function Connect()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// AddBroadCasters()
	// Input parameters are the nymname and the strbroadcasters
	//Adds the selected broadcaster to the appropriate nym
	// returns 1 if addition was done succesfully else returns a 0.
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static int AddBroadCasters(String strNymName,String strBroadCasters)
	{
		ResultSet rsBroadCasters;
		Connection conn=Connect();
		int iBroadCasterID=0;
		String strNymID=GetNymID(strNymName);
		//get the names of the broadcasters by tokenizing the string
		StringTokenizer strTokBroadCasterName=new StringTokenizer(strBroadCasters,",");
		try
		{
			Statement stAddBroadCasterName=conn.createStatement();
			Statement stBroadCasterID=conn.createStatement();
			//while all the selected broadcasters are to be added.
			while(strTokBroadCasterName.hasMoreTokens())
			{
				//Query to obtain the broadcasters ID
				rsBroadCasters=stBroadCasterID.executeQuery("select BroadCasterID from broadcasters where BroadCasterName= '"+strTokBroadCasterName.nextToken()+"'"); 
				while(rsBroadCasters.next())
				{
					iBroadCasterID=rsBroadCasters.getInt("BroadCasterID");
				}
				//Add broadcaster to the nym
				stAddBroadCasterName.executeUpdate("insert into broadcasternymmap values('"+iBroadCasterID+"','"+strNymID+"')"); 
			}
			//return success as 1
			return 1;
		}
		catch(Exception e)
		{
			System.out.println(" The following Exception occured"+e);
			//return success as 0
			return 0;
		}
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// end of function AddBroadCasters()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// GetNymID()
	// Gets the 128 bit NymID corresponding to a nym using the nym name as the parameter
	// returns 128 bit NymID
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	public static String GetNymID(String strNym)
	{
		Connection conn =Connect();
		String strNymid=null;
		ResultSet rsNymID;
		try
		{
			Statement stgetNymID= conn.createStatement();
		      //Get the nymid corresponding to the nym name from the database 
			rsNymID=stgetNymID.executeQuery("select nymid from nymnamemap where nymname='" + strNym + "'");
			while( rsNymID.next() )
			{
				strNymid=rsNymID.getString("nymid");
			}
		}
		catch( Exception e )
		{
			System.out.println ( "Database Exception is " + e );
		};  
		//return the nymid
		return(strNymid);
	}	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// end of function GetNymID()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
		
	public void destroy() 
	{
		System.out.println("AddBroadCasterServlet: destroy()");
	}
}